package com.mcr.reggie.controller;

import com.mcr.reggie.common.R;
import com.mcr.reggie.entity.AddressBook;
import com.mcr.reggie.service.AddressBookService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    /**
     *
     *
     *
     * ****************************** 前台  ****************************************************
     *
     *
     */
    @ApiOperation(value = "用户新增地址")
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBookService.saveAddressBook(addressBook);
        return R.success(addressBook);
    }
    @ApiOperation(value = "设置默认地址")
    @PutMapping("default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.setDefaultAddressBook(addressBook);
        return R.success(addressBook);
    }
    @ApiOperation(value = "根据id查询地址")
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        AddressBook addressBook =addressBookService.getAddressBookById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }
    @ApiOperation(value = "查询默认地址")
    @GetMapping("default")
    public R<AddressBook> getDefault() {
        AddressBook addressBook =addressBookService.getDefaultAddressBook();
        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }
    @ApiOperation(value = "查询指定用户的全部地址")
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        List<AddressBook> addressBookList=addressBookService.listAddressBook();
        return R.success(addressBookList);
    }
    @ApiOperation(value = "用户修改地址")
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("地址信息修改成功");
    }
}
